package com.sgcc.serviceprovider.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @BelongsProject: springcloud
 * @BelongsPackage: com.sgcc.serviceprovider.controller
 * @Author: LiMengshuai
 * @CreateTime: 2019-07-24 09:48
 * @Description: ${Description}
 */
@RestController
public class ProviderController {

    @Value("${server.port}")
    private String port;

    @Autowired
    private DiscoveryClient client;

    @RequestMapping("/hello/{string}")
    @HystrixCommand(fallbackMethod = "error")
    public String hello(@PathVariable String string) {

        if("error".equals(string)){
            throw new RuntimeException("有问题！！！");
        }
        return "主机端口：" + port + "  输出：" + string;
    }

    @RequestMapping("/discovery")
    public Object discovery() {
        List<String> list = client.getServices();
        System.out.println("#########" + list);

        List<ServiceInstance> serList = client.getInstances("SERVICE-PROVIDER");
        for (ServiceInstance element : serList) {
            System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t"
                    + element.getPort() + "\t" + element.getUri());
        }
        return this.client;

    }

    public String error(@PathVariable String string){
        return " client error!!!    "+string;
    }

}
